package com.gxdf.extendedcontrols.gxdfclient;

import com.artech.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class GxdfMapActivity extends FragmentActivity {

	final String SELECTED_LOCATION = "selected_location";
	GoogleMap map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gxdf_map);

		if (map == null) {
			map = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.gxdfmapview)).getMap();
			configurarMapa();
		}

	}

	private void configurarMapa() {

		map.setMyLocationEnabled(true);
		LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		String provider = locationManager.getBestProvider(criteria, true);

		Location myLocation = locationManager.getLastKnownLocation(provider);

		if (myLocation != null) {
			map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
			double lat = myLocation.getLatitude();
			double lon = myLocation.getLongitude();
			LatLng latlng = new LatLng(lat, lon);

			map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
			map.animateCamera(CameraUpdateFactory.zoomTo(20));
			map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
					.title("Usted se encuentra aqui"));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();

		Intent resultIntent = new Intent();

		String latitude = String.valueOf(map.getMyLocation().getLatitude());
		String longitud = String.valueOf(map.getMyLocation().getLongitude());

		if (latitude.equals(null))
			latitude = "";

		resultIntent.putExtra(SELECTED_LOCATION, latitude + "," + longitud);
		
		setResult(Activity.RESULT_OK, resultIntent);
		

		Log.i("MapActivity", latitude);
		finish();
	}
	
	
	
	

}
